 /*      SClassFloat.cpp
 *
 *      Copyright 2009 virpool <virpool@ukrpost.ua>
 *
 *      This program is free software; you can redistribute it and/or modify
 *      it under the terms of the GNU General Public License as published by
 *      the Free Software Foundation; either version 2 of the License, or
 *      (at your option) any later version.
 *
 *      This program is distributed in the hope that it will be useful,
 *      but WITHOUT ANY WARRANTY; without even the implied warranty of
 *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *      GNU General Public License for more details.
 *
 *      You should have received a copy of the GNU General Public License
 *      along with this program; if not, write to the Free Software
 *      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 *      MA 02110-1301, USA.
 */

#include "SClassFloat.h"

SClassFloat::SClassFloat(string name): SObject(name, "Float")
{
	GetMethods()->erase("ToString");
	GetMethods()->insert(pair<string, s_func>("ToString", &SClassFloat::ToString));
	GetMethods()->insert(pair<string, s_func>("Rand", &SClassFloat::Rand));
	GetMethods()->insert(pair<string, s_func>("Sqr", &SClassFloat::Sqr));
	GetMethods()->insert(pair<string, s_func>("Step", &SClassFloat::Step));
	GetMethods()->insert(pair<string, s_func>("Step", &SClassFloat::Fixed));
}

int SClassFloat::ToString(SObject* obj, s_param* res, param_list* args)
{
	if (obj == NULL || res == NULL)
	{
		return S_INVOKE_INCORRECT;
	}
	res->type = S_STRING;
	string *s = new string();
	ostringstream ostr;
	ostr << ((SClassFloat*)obj)->Get();
	(*s) = ostr.str();
	res->val = s;
	return S_INVOKE_OK;
}

int SClassFloat::Rand(SObject* obj, s_param* res, param_list* args)
{
	static string r = "Invoke ok.";
	cerr << "in method Rand\n";
	res->type = S_STRING;
	res->val = &r;
	((SClassFloat*)obj)->Set(rand());
	return S_INVOKE_OK;
}

int SClassFloat::Sqr(SObject* obj, s_param* res, param_list* args)
{
	static string r = "Invoke ok.";
	cerr << "in method Sqr\n";
	res->type = S_STRING;
	res->val = &r;
	((SClassFloat*)obj)->Set(((SClassFloat*)obj)->Get() * ((SClassFloat*)obj)->Get());
	return S_INVOKE_OK;
}

int SClassFloat::Step(SObject* obj, s_param* res, param_list* args)
{
	static string r = "Invoke ok.";
	cerr << "in method Step\n";
	if (args->size() == 0)
		return S_INVOKE_INCORRECT;
	param_list::iterator p = args->begin();
	if (p->type != S_INT)
		return S_INVOKE_INCORRECT;
	int v = *((int*)p->val);
	double rr = ((SClassFloat*)obj)->Get();
	cerr << "step = " << v << endl;
	res->type = S_STRING;
	res->val = &r;
	for (int i = 1; i < v; i++)
	{
		rr *= ((SClassFloat*)obj)->Get();
	}
	((SClassFloat*)obj)->Set( rr );
	return S_INVOKE_OK;
}

int SClassFloat::Fixed(SObject* obj, s_param* res, param_list* args)
{
	static string r = "Invoke ok.";
	cerr << "in method Fixed\n";
	if (args->size() == 0)
		return S_INVOKE_INCORRECT;
	param_list::iterator p = args->begin();
	if (p->type != S_INT)
		return S_INVOKE_INCORRECT;
	return S_INVOKE_OK;
}

